<?php
/**
 * DynaGrid - The PHP datagrid
 *
 * @package    DynaGrid
 * @author     $Author: keesschepers $
 * @version    $Id: Columns.php 15 2011-02-15 17:07:57Z keesschepers $
 */

class DynaGrid_HTML_Table_Columns extends DynaGrid_Collection_Object {
	/**
	 * Contains all data and meta information like columns
	 *
	 * @var DynaGrid_DataSource
	 */
	public $datasource;
	/**
	 * Only way to make it possible to manually sort dynamic columns, to save them seperatly from the normal columns.
	 *
	 * @var array
	 */
	protected $dynamiccolumns = array();
	/**
	 * Nasty solution to check wether columns from the dataset are already imported.
	 *
	 * @var boolean
	 */
	private $dataimported = false;
	public function __construct(DynaGrid_DataSource &$datasource) {
		$this->datasource = $datasource;
	}
	public function exists($column) {
		return array_key_exists($column,$this->items);
	}
	public function add($name) {		
		$this->items[$name]['header'] = DynaGrid_HTML_Table_Column::factory($name,'header');
		$this->items[$name]['body'] = DynaGrid_HTML_Table_Column::factory($name,'body');
		$this->items[$name]['footer'] = DynaGrid_HTML_Table_Column::factory($name,'footer');
		$this->items[$name]['position'] = 0;
		$this->items[$name]['name'] = $name;
	}
	public function addDynamic($name,$position='root') {
		$this->dynamiccolumns[$position][$name]['header'] = DynaGrid_HTML_Table_Column::factory($name,'header');
		$this->dynamiccolumns[$position][$name]['body'] = DynaGrid_HTML_Table_Column::factory($name,'body');
		$this->dynamiccolumns[$position][$name]['footer'] = DynaGrid_HTML_Table_Column::factory($name,'footer');
		$this->dynamiccolumns[$position][$name]['name'] = $name;
	}
	/**
	 * Returns the columns. The user has to specify which part is returned. Columns for the footer, header or
	 * body part of the table.
	 *
	 * @param string $part
	 * @return array itemslist (numeric indexed array)
	 */
	public function getColumns($part='body') {
		if(!$this->dataimported) {
			$position = 0;
			foreach ($this->datasource->getColumns() as $columnName) {
				if(!$this->exists($columnName)) {
					$this->add($columnName);
				}
				
				$this->items[$columnName]['position'] = $position;
				$position++;
			}
			
			uasort($this->items,array($this,'orderColumns'));
			$this->dataimported = true;
		}
		
		$items = array();				
		if(array_key_exists('root',$this->dynamiccolumns)) {
			foreach ($this->dynamiccolumns['root'] as $dynamiccolumns) {
				$items[] = $dynamiccolumns[$part];
			}			
		}
		
		foreach ($this->items as $columnName=>$columns) {			
			if(array_key_exists($part,$columns)) {				
				$items[] = $this->items[$columnName][$part];
				
				if(array_key_exists($columnName,$this->dynamiccolumns)) {
					foreach ($this->dynamiccolumns[$columnName] as $dynamiccolumns) {
						$items[] = $dynamiccolumns[$part];
					}
				}
			}
		}
		return $items;
	}
  private function orderColumns($firstColumn,$secondColumn) {  	
  	if($firstColumn['position']==$secondColumn['position']) {
  		return 0;
  	} else {
  		return ($firstColumn['position']>$secondColumn['position']) ? +1 : -1;
  	}
  }
	public function setColumnPosition($column,$position) {
		$this->items[$column]['position'] = $position;
	}
	public function getDynamicColumn($name,$part='header') {		
		if(array_key_exists($name,$this->dynamiccolumns)) {
			if(array_key_exists($part,$this->items[$name])) {
				return $this->items[$name][$part];
			}
		}
		
		return false;
	}	
	public function getColumn($name,$part='header') {		
		if(array_key_exists($name,$this->items)) {
			if(array_key_exists($part,$this->items[$name])) {
				return $this->items[$name][$part];
			}
		}
		
		return false;
	}
}